package com.jy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.home.AddOrEditSwiperPamras;
import com.jy.entity.Goods;
import com.jy.entity.MainPageSwiper;
import com.jy.mapper.GoodsMapper;
import com.jy.mapper.MainPageSwiperMapper;
import com.jy.service.MainPageSwiperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2024-11-12
 */
@Service
public class MainPageSwiperServiceImpl extends ServiceImpl<MainPageSwiperMapper, MainPageSwiper> implements MainPageSwiperService {

    private final MainPageSwiperMapper mainPageSwiperMapper;
    private final GoodsMapper goodsMapper;


    public MainPageSwiperServiceImpl(MainPageSwiperMapper mainPageSwiperMapper, GoodsMapper goodsMapper) {
        this.mainPageSwiperMapper = mainPageSwiperMapper;
        this.goodsMapper = goodsMapper;
    }

    @Override
    public ResponseWrapper getSwiperImage() {
        // 查询出让展示的轮播图
        List<MainPageSwiper> swipers = mainPageSwiperMapper.selectList(
                new LambdaQueryWrapper<MainPageSwiper>()
                        .eq(MainPageSwiper::getIsShowApp,true)
                        .orderByDesc(MainPageSwiper::getUpdateTime)
                        .last("limit 5")
        );

        ArrayList<HashMap<String, Object>> result = new ArrayList<>();
        for (MainPageSwiper swiper : swipers) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("aid",swiper.getAid());
            map.put("name",swiper.getName());
            map.put("url",swiper.getUrl());

            if(swiper.getType() != null){
                map.put("type",swiper.getType());
                if(swiper.getType().equals("商品")){
                    Goods goods = goodsMapper.selectOne(new LambdaQueryWrapper<Goods>().eq(Goods::getGoodsCode, swiper.getSwiperCode()));
                    if (goods != null){
                        map.put("price","￥" + goods.getGoodsPrice());
                    }else {
                        map.put("price","");
                    }
                }else {
                    map.put("price","");
                }
            }else {
                map.put("type","");
                map.put("price","");
//                map.put("isCollect","");
            }
            result.add(map);
        }

        return ResponseWrapper.markSuccess(result);

    }

    @Override
    public ResponseWrapper getSwiperImageLisT() {
        List<MainPageSwiper> swipers = mainPageSwiperMapper.selectList(
                new LambdaQueryWrapper<MainPageSwiper>()
                .orderByDesc(MainPageSwiper::getUpdateTime)
        );
        return ResponseWrapper.markSuccess(swipers);
    }

    @Override
    public ResponseWrapper addOrEditSwiper(AddOrEditSwiperPamras pamras) {
        // 根据swiperType获取对应的typeId，使用Java 8的方式替代Map.of
        Map<String, Integer> typeIdMap = new HashMap<>();
        typeIdMap.put("商品", 1);
        typeIdMap.put("酒店", 2);
        typeIdMap.put("文章", 3);
        typeIdMap.put("资讯", 4);
        Integer typeId = typeIdMap.getOrDefault(pamras.getSwiperType(), 0);

        // 判断是添加还是编辑
        if ("添加".equals(pamras.getAddOrEditText())) {
            MainPageSwiper newSwiper = new MainPageSwiper();
            setSwiperProperties(newSwiper, pamras, typeId);
            mainPageSwiperMapper.insert(newSwiper);
        } else {
            // 编辑
            LambdaQueryWrapper<MainPageSwiper> queryWrapper = new LambdaQueryWrapper<MainPageSwiper>()
                    .eq(MainPageSwiper::getName, pamras.getSwiperName())
                    .eq(MainPageSwiper::getType, pamras.getSwiperType())
                    .eq(MainPageSwiper::getSwiperCode, pamras.getSwiperCode());
            List<MainPageSwiper> swipers = mainPageSwiperMapper.selectList(queryWrapper);

            MainPageSwiper mainPageSwiper;
            if (swipers == null || swipers.isEmpty()) {
                // 如果不存在就新增
                mainPageSwiper = new MainPageSwiper();
                setSwiperProperties(mainPageSwiper, pamras, typeId);
                mainPageSwiperMapper.insert(mainPageSwiper);
            } else {
                // 如果存在就更新
                mainPageSwiper = swipers.get(0);
                setSwiperProperties(mainPageSwiper, pamras, typeId);
                mainPageSwiperMapper.updateById(mainPageSwiper);
            }
        }
        return ResponseWrapper.markSuccess();
    }

    // 提取设置MainPageSwiper属性的方法，避免重复代码
    private void setSwiperProperties(MainPageSwiper mainPageSwiper, AddOrEditSwiperPamras pamras, Integer typeId) {
        mainPageSwiper.setName(pamras.getSwiperName());
        mainPageSwiper.setType(pamras.getSwiperType());
        mainPageSwiper.setUrl(pamras.getSwiperImgUrl());
        mainPageSwiper.setTypeId(typeId);
        mainPageSwiper.setSwiperCode(pamras.getSwiperCode());
        mainPageSwiper.setDescription(pamras.getDescription());
        mainPageSwiper.setIsShowApp(pamras.getIsShowApp());
    }

    @Override
    public ResponseWrapper getCode(String typeName) {

//        System.out.println(typeName);

        if(typeName.equals("商品")){
            // 获取所有商品的编码
            return ResponseWrapper.markSuccess(goodsMapper.getDistinctGoodsCode());
        }else {
            return ResponseWrapper.markSuccess(new ArrayList<>());
        }

    }

    @Override
    public ResponseWrapper deleteSwiperByAid(Integer swiperAid) {

        int delete = mainPageSwiperMapper.deleteById(swiperAid);
        if (delete > 0) {
            return ResponseWrapper.markSuccess();
        }else {
            return ResponseWrapper.markError();
        }
    }
}
